<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-03-15
 * Time: 11:48
 */
namespace app\admin\controller;

use think\Db;
use think\Exception;

class Sys extends Base{

    /**
     * 后台菜单列表
     */
    public function admin_rule_list()
    {
        $pid=input('pid',0);
        $level=input('level',0);
        $id_str=input('id','pid');
        $admin_rule=Db::name('auth_rule')->where('pid',$pid)->order('sort')->select();
        $admin_rule_all=Db::name('auth_rule')->order('sort')->select();
        $arr = menu_left($admin_rule,'id','pid','─',$pid,$level,$level*20);
        $arr_all = menu_left($admin_rule_all,'id','pid','─',0,$level,$level*20);
        $this->assign('admin_rule',$arr);
        $this->assign('admin_rule_all',$arr_all);
        $this->assign('pid',$id_str);
        if(request()->isAjax()){
            return $this->fetch('ajax_admin_rule_list');
        }else{
            return $this->fetch();
        }
    }

    /**
     * 热门搜索列表
     */
    public function hot_word(){
        $page_param = input('param.');
        if (isset($page_param['page']) && $page_param['page'] > 0)
            unset($page_param['page']);

        $word_list = Db::name('hot_word')->order('sort asc')->paginate('20');
        //分页信息
        $page = $word_list->render();
        $word_list = $word_list->toArray();
        //模板变量赋值
        $this->assign('list', $word_list['data']);
        $this->assign('total', $word_list['total']);
        $this->assign('page', $page);

        return $this->fetch();
    }

    /**
     * 添加热门搜索词
     */
    public function word_add(){
        if ($this->request->isPost()){
            Db::startTrans();
            try{
                $word = trim(input('param.word',''));
                if (empty($word)){
                    throw new Exception('热词不能为空');
                }
                $sort = input('param.sort','50','intval');
                $flag = Db::name('hot_word')->insert(['word'=>$word,'sort'=> $sort,'add_time'=>time()]);
                if ($flag!= false){
                    Db::commit();
                    return json(['code'=>200,'msg'=>'success']);
                }
            }catch (Exception $e){
                Db::rollback();
                return json(['code'=>0,'msg'=>$e->getMessage()]);
            }
        }else{
            // 渲染模板输出
            return $this->fetch();
        }
    }

    /**
     * 热词删除
     */
    public function word_del(){
        $word_id=input('id','','intval');
        Db::startTrans();
        try{
            if (!is_numeric($word_id)){
                throw new Exception('热词id错误');
            }
            $rst=Db::name('hot_word')->where('id',$word_id)->delete();
            if($rst!==false){
                Db::commit();
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                throw new Exception('删除失败');
            }
        }catch (Exception $e){
            Db::rollback();
            return json(['code'=>0,'msg'=>$e->getMessage()]);
        }
    }


    //热搜排序
    public function word_order(){
        if (!$this->request->isPost()){
            return json(['code'=>0,'msg'=>'提交方式不正确']);
        }else{
            foreach ($_POST as $id => $sort){
                Db::name('hot_word')->where(array('id' => $id ))->setField('sort' , $sort);
            }
            return json(['code'=>200,'msg'=>'排序更新成功']);
        }
    }


    /**
     * 淘宝授权登录
     */
    public function tb_login(){
        return $this->fetch();
    }
    /**
     * 授权
     */
    public function tbauth(){
        $url = 'https://oauth.taobao.com/authorize?response_type=code&client_id=24635648&redirect_uri=http://admin.vipbuy360.com/admin/&state=1212&view=web';
        $this->redirect($url);
    }



}